# Maintainer: Jakub Jirutka <jakub@jirutka.cz>
pkgname=dhcp
pkgver=4.4.3
_realver=${pkgver/_p/-P}
pkgrel=0
pkgdesc="ISC Dynamic Host Configuration Protocol (DHCP)"
url="https://www.isc.org/"
arch="all"
license="MPL-2.0"
_depends_server_ldap="!$pkgname-server-vanilla $pkgname"
_depends_server_vanilla="!$pkgname-server-ldap $pkgname"
depends_static="$pkgname-dev"
# busybox utils are not sufficient
_depends_dhclient="coreutils iproute2 run-parts"
_depends_dhcrelay=""  # do not inherit depends from $pkgname
pkgusers="dhcp"
pkggroups="dhcp"
makedepends="krb5-dev linux-headers openldap-dev perl"
install="$pkgname.pre-install"
subpackages="
	$pkgname-dbg
	$pkgname-doc
	$pkgname-openrc
	$pkgname-libs-static
	$pkgname-dev
	dhclient
	dhcrelay
	dhcrelay-openrc
	$pkgname-server-vanilla:server_vanilla
	$pkgname-server-ldap:server_ldap
	"
source="
	https://downloads.isc.org/isc/dhcp/$_realver/dhcp-$_realver.tar.gz
	01-dhclient-script-fix-bare-ip.patch
	02-dhclient-script-remove-bashisms.patch
	03-fix-unwind-import.patch
	dhcp-3.0-fix-perms.patch
	dhclient-script-alpine
	dhcrelay.initd
	dhcrelay.confd
	dhcpd.confd
	dhcpd.initd
	"
builddir="$srcdir/$pkgname-$_realver"
makedepends="$makedepends $_depends_dhclient $_depends_server_ldap $_depends_server_vanilla"

# secfixes:
#   4.4.2_p1-r0:
#     - CVE-2021-25217
#   4.4.1-r0:
#     - CVE-2019-6470
#     - CVE-2018-5732
#     - CVE-2018-5733

_configure() {
	# fix ipv6
	export CFLAGS="$CFLAGS -D_GNU_SOURCE \
		-D_PATH_DHCLIENT_CONF='\"/etc/dhcp/dhclient.conf\"'"

	./configure \
		--build="$CBUILD" \
		--host="$CHOST" \
		--prefix=/usr \
		--sysconfdir=/etc/dhcp \
		--localstatedir=/var \
		--with-srv-conf-file=/etc/dhcp/dhcpd.conf \
		--with-srv-lease-file=/var/lib/dhcp/dhcpd.leases \
		--with-srv6-lease-file=/var/lib/dhcp/dhcpd6.leases \
		--with-cli-lease-file=/var/lib/dhcp/dhclient.leases \
		--with-cli6-lease-file=/var/lib/dhcp/dhclient6.leases \
		--with-srv-pid-file=/run/dhcp/dhcpd.pid \
		--with-srv6-pid-file=/run/dhcp/dhcpd6.pid \
		--with-cli-pid-file=/run/dhcp/dhclient.pid \
		--with-cli6-pid-file=/run/dhcp/dhclient6.pid \
		--with-relay-pid-file=/run/dhcp/dhcrelay.pid \
		--with-relay6-pid-file=/run/dhcp/dhcrelay6.pid \
		--with-libbind=no \
		--enable-dhcpv4o6 \
		--enable-paranoia \
		--enable-log-pid \
		"$@"

}

prepare() {
	# Normally the upstream build system unpacks this before building the
	# library but we need to patch it for segfualt-fix.patch. If we unpack
	# it early then the build system will just use what we've already
	# unpacked and patched.
	tar -C "$builddir"/bind/ -xzf "$builddir"/bind/bind.tar.gz
	default_prepare
	update_config_guess
	update_config_sub
}

build() {
	cp config.guess config.sub bind/bind-*/
	cp -a "$builddir" "$builddir-ldap"

	_configure
	make -j1 -C bind && make

	cd "$builddir-ldap"
	_configure \
		--with-ldap \
		--with-ldapcrypto \
		--with-ldap-gssapi
	make -j1 -C bind && make
}

check() {
	make check

	cd "$builddir-ldap"
	make check
}

package() {
	make DESTDIR="$pkgdir" install

	#install -m751 -D "$builddir"/client/scripts/linux "$pkgdir"/sbin/dhclient-script
	install -m751 -D "$srcdir"/dhclient-script-alpine "$pkgdir"/sbin/dhclient-script
	install -d "$pkgdir"/var/lib/dhcp
	install -d "$pkgdir"/run/dhcp
}

openrc() {
	local name="${subpkgname%-openrc}"
	[ "$name" = dhcp ] && name="dhcpd"

	install -m755 -D "$srcdir"/$name.initd "$pkgdir"/etc/init.d/$name
	install -m644 -D "$srcdir"/$name.confd "$pkgdir"/etc/conf.d/$name

	default_openrc
}

server_ldap() {
	pkgdesc="ISC dhcpd server with LDAP support"
	depends="$_depends_server_ldap"
	provides="dhcp-server"
	provider_priority=100

	install -d "$subpkgdir"
	make DESTDIR="$subpkgdir" -C "$builddir-ldap/server" install-sbinPROGRAMS
}

server_vanilla() {
	pkgdesc="ISC dhcpd server"
	depends="$_depends_server_vanilla"
	install_if="$pkgname=$pkgver-r$pkgrel"
	provides="dhcp-server"
	provider_priority=200

	install -d "$subpkgdir"/usr/sbin
	mv "$pkgdir/usr/sbin/dhcpd" "$subpkgdir/usr/sbin/"
	rmdir "$pkgdir/usr/sbin" || :
}

dhclient() {
	pkgdesc="ISC dhcp client"
	depends="$_depends_dhclient"

	install -d -o dhcp -g dhcp "$subpkgdir"/var/lib/dhcp
	install -d -o dhcp -g dhcp "$subpkgdir"/run/dhcp
	install -d "$subpkgdir"/usr/sbin
	install -d "$subpkgdir"/sbin
	install -d "$subpkgdir"/etc/dhcp
	mv "$pkgdir"/usr/sbin/dhclient "$subpkgdir"/usr/sbin/
	mv "$pkgdir"/sbin/dhclient-script "$subpkgdir"/sbin/
	ln -s ../sbin/dhclient-script "$subpkgdir"/etc/dhclient-script
	mv "$pkgdir"/etc/dhcp/dhclient.conf* "$subpkgdir"/etc/dhcp/
}

dhcrelay() {
	pkgdesc="ISC dhcp relay server"
	depends="$_depends_dhcrelay"
	replaces="dhcp"
	install="$subpkgname.pre-install"

	install -d "$subpkgdir"/run/dhcp \
		"$subpkgdir"/usr/sbin
	mv "$pkgdir"/usr/sbin/dhcrelay "$subpkgdir"/usr/sbin/
}

static() {
	depends="$depends_static"
	default_static || mkdir -p "$subpkgdir"
}


# gpg_signature_extensions="sha512.asc"
# gpgfingerprints="
# 	good:AE3F AC79 6711 EC59 FC00  7AA4 74BB 6B9A 4CBB 3D38
# 	BE0E 9748 B718 253A 28BB  89FF F1B1 1BF0 5CF0 2E57
# 	"

sha512sums="
4472d6794af80b482560956cee6895889cc1aca39980f851faf56824627e95731f2983cf7c7454bc3decb0a12c874fcbd29bd6c5a9695412def6bc14c6df17e0  dhcp-4.4.3.tar.gz
17e2b9588ee5d1bd9acb9c2e30f7a28308d29c9e797c2be14c1feff52e6e231ce8a94535f18badff1342aff4ae4003aab986e0f0473f0cd280292fdab044b148  01-dhclient-script-fix-bare-ip.patch
a70e4a7e80ee65c8ced6b61db80f7ccd0f35015b5cccf2e7c51705ae129230aa49ba9926bb88f7418018e7a112c2a40451f24b88e04464b590ff20091e8d8709  02-dhclient-script-remove-bashisms.patch
23ab581d85ba97a37fd6a0a612e0aa977b24bbaf83d58a93d1a87f9f24ea9a098aa549e77a6e1d78f721681c152464b15fd1d402d0673edf4dac6aa196df1fe9  03-fix-unwind-import.patch
d5697a56fbbff25199962608986e7ffb533ed4afd3e344e3c79d2010dda73cc0b088f06c454e9f0c69eb054e09a374455fa71d3f73306e0c98fa76df4dd321b7  dhcp-3.0-fix-perms.patch
d1dce58875793316761f168e29feddc1d3454d1d917d063d43ae102b7b6aab256c3cb420478335c57ebcdb2b7c804afa4d8a1f9ab06a29a4dd23bc5d87db8df2  dhclient-script-alpine
ce62693cb483616844bb6774f9046af6a1a210e35cfaa59ab3bd12f68d50176714a324e92538b35139110b78191866f65b30d6979d8a45f7b68e572e7a1e8427  dhcrelay.initd
fd15dbaa4c61c3c26f407bf13dde859470a1adba134da064b653ccc152ce42635ee8de2fe113ae21ba8470e97e3caad8c1a47b69eb25e5e92b40e26790b96f6d  dhcrelay.confd
7b7a77b7826b475a4113ebeee54501ce417cc56e85754301a82a185d88b4713d198f615a366e63e0e2b0aef988c8137dcd1e18c4036d993378257079da17693d  dhcpd.confd
1f78c4f64a891012b4de029c50934b284ad80313959e06b0f29416f33a9e4f0deca8b61e9224a683a92faf8a08e8b7c075bc6e833ab73b6305c0d6d9712d0baf  dhcpd.initd
"
